$().ready(function () {
    $('#searchable').multiselect2side({
        // search: "医院列表   查询：",
        selectedPosition: 'right',
        moveOptions: false,
        labelsx: '医院列表',
        labeldx: '授权医院'
    });
});


$(function(){
    doOnSelectInit();
});

//初始化选择界面
function doOnSelectInit() {
    var str = '';
    var selectedStr = '';
    var allHos = parent.hosDtoList;
    if (allHos == null) return;
    if (hosCodes == null) {
        for (var i=0; i<allHos.length; i++) {
            str += "<option value='"+allHos[i].hos_code+"'>"+allHos[i].hos_name+"</option>";
        }
    } else {
        for (var i=0; i<allHos.length; i++) {
            var type = false;
            for (var j=0; j<hosCodes.length; j++) {
                if (allHos[i].hos_code == hosCodes[j]) {
                    type = true;
                    continue;
                }
            }
            if (type) {
                selectedStr += "<option value='"+allHos[i].hos_code+"'>"+allHos[i].hos_name+"</option>";
            } else {
                str += "<option value='"+allHos[i].hos_code+"'>"+allHos[i].hos_name+"</option>";
            }
        }
    }
    $("#search_ablems2side__sx").append(str);
    $("#search_ablems2side__dx").append(selectedStr);

}

function doOnButtFormInit() {
    if (typeof (editForm1) == 'object') {
        editForm1.attachEvent("onButtonClick",function(name,command) {
            if (command == 'save')//保存
                doSave(command);
            else if (command == 'cancle')//关闭
                doCancle();
        });
    }
}

//取消动作
function doCancle() {
    parent.authorityWin.close();
}

//保存
function doSave(command) {
    if (command != 'save') return;
    var selectedElements = $("#search_ablems2side__dx").children();
    var hCodes = new Array();
    for (var i=0; i<selectedElements.length; i++) {
        var hos_code = selectedElements[i].value;
        hCodes.push(encodeURIComponent(hos_code));
    }
    var alipayId = parent.oneAdminInfo.alipay_id;
    var postData = "hosCodes="+hCodes+"&alipayId="+alipayId;
    $.ajax({
        url: basePath+'managerAlipayUser/saveUserHosRef',
        type: 'post',
        dataType: 'json', //返回的数据类型
        data:encodeURI(postData),
        success: function (data) {
            if (!data) {
                $func.alert("保存失败！", "red");
                return;
            }
            if (!data.result) {
                $func.alert("保存失败！"+data.message, "red");
                return;
            }
            if (data.result) {
                $func.alert("保存成功！", "green");
                doCancle();
            }
        }
    });
}

function doselectedFormInit() {
    if (typeof(selectForm) == "object"){
        selectForm.attachEvent("onButtonClick", function(name, command) {
            switch(command) {
                case 'select':select();break;
            }
        });
    }
}
function select() {
    var searchName = $.trim(selectForm.getInput("authorityId").value);
    if(searchName.length>0){
        $("#search_ablems2side__sx").children().hide();
        $("option:hidden:contains("+searchName+")") .show();

    }
    if (searchName.length == 0) {
        $("#search_ablems2side__sx").children().show();
    }
}

/* 登录框enter事件 */
$(document).keypress(function(e){
    if (e.keyCode == "13") {
        select();
    }
});
